package com.hmall.cart.listen;

import com.hmall.api.dto.OrderDetailDTO;
import com.hmall.cart.service.ICartService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Component
@RequiredArgsConstructor
@Slf4j
public class DeleteCartItemsListener {

    private final ICartService iCartService;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "trade.cart.success.queue", durable = "true"),
            exchange = @Exchange(name = "tradecart.direct"),
            key = "tradecart.success"
    ))
    public void deleteCartItems(List<Long> ids)
    {
        log.info("接收到购物车清理消息，商品ID列表: {}", ids);
        try {
            if (ids != null && !ids.isEmpty()) {
                iCartService.removeByItemIds(new HashSet<>(ids));
                log.info("购物车清理完成，商品ID列表: {}", ids);
            } else {
                log.warn("接收到空的商品ID列表");
            }
        } catch (Exception e) {
            log.error("购物车清理失败，商品ID列表: {}", ids, e);
            throw new RuntimeException("购物车清理失败", e);
        }
    }
}
